/*
 * 
 * 
 * 
 */
package com.hboxs.asl.service.impl;

import com.hboxs.asl.*;
import com.hboxs.asl.Order;
import com.hboxs.asl.dao.*;
import com.hboxs.asl.entity.*;
import com.hboxs.asl.entity.Review.Type;
import com.hboxs.asl.service.ReviewService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Service - 评论
 */
@Service("reviewServiceImpl")
public class ReviewServiceImpl extends BaseServiceImpl<Review, Long> implements ReviewService {

    @Resource(name = "reviewDaoImpl")
    private ReviewDao reviewDao;
    @Resource(name = "orderItemDaoImpl")
    private OrderItemDao orderItemDao;
    @Resource(name = "courseTradeDaoImpl")
    private CourseTradeDao courseTradeDao;

    @Resource(name = "reviewDaoImpl")
    public void setBaseDao(ReviewDao reviewDao) {
        super.setBaseDao(reviewDao);
    }

    @Override
    public Double serviceScore(Member member) {
        return reviewDao.serviceScore(member);
    }

    @Override
    public Double schemeScore(Member member) {
        return reviewDao.schemeScore(member);
    }

    @Override
    public Double feedbackScore(Member member) {
        return reviewDao.feedbackScore(member);
    }

    @Transactional(readOnly = true)
    public Page<Review> myCommemtPage(Pageable pageable, Member member) {
        return reviewDao.myCommemtPage(pageable, member);
    }

    @Override
    public List<Review> myCommentList(Member teacher, Integer count, List<Filter> filters, List<Order> orders) {
        return reviewDao.myCommentList(teacher, count, filters, orders);
    }

    @Override
    public long myReviews(Member teacher) {
        return reviewDao.myReviews(teacher);
    }

    @Override
    public Page<Review> reviewPage(Pageable pageable, Member teacher) {
        return reviewDao.reviewPage(pageable, teacher);
    }

    @Override
    public void addComment(Member member, Course course, CourseTrade courseTrade, Review review) {
        review.setMember(member);
        review.setCourse(course);
        courseTrade.setHasReview(true);
        courseTradeDao.persist(courseTrade);
        save(review);
    }

    @Transactional(readOnly = true)
    public List<Review> findList(Member member, Product product, Type type, Boolean isShow, Integer count, List<Filter> filters, List<Order> orders) {
        return reviewDao.findList(member, product, type, isShow, count, filters, orders);
    }

    @Transactional(readOnly = true)
    @Cacheable("review")
    public List<Review> findList(Member member, Product product, Type type, Boolean isShow, Integer count, List<Filter> filters, List<Order> orders, String cacheRegion) {
        return reviewDao.findList(member, product, type, isShow, count, filters, orders);
    }

    @Transactional(readOnly = true)
    public Page<Review> findPage(Member member, Product product, Type type, Boolean isShow, Pageable pageable) {
        return reviewDao.findPage(member, product, type, isShow, pageable);
    }

    @Transactional(readOnly = true)
    public Page<Review> findPageGroup(Member member, Product product, Type type, Boolean isShow, Pageable pageable) {
        return reviewDao.findPageGroup(member, product, type, isShow, pageable);
    }

    @Transactional(readOnly = true)
    public Long count(Member member, Product product, Type type, Boolean isShow) {
        return reviewDao.count(member, product, type, isShow);
    }

    @Transactional(readOnly = true)
    public boolean isReviewed(Member member, Product product) {
        return reviewDao.isReviewed(member, product);
    }

    @Override
    public List<Review> lastThreeReviews(Product product) {
        return reviewDao.lastThreeReviews(product);
    }

    @Override
    public void create(Member member, OrderItem oi, String remoteAddr, Review review) {
        review.setIp(remoteAddr);
        review.setIsShow(true);
        review.setMember(member);
        oi.setHasReview(true);
        orderItemDao.merge(oi);
        save(review);
    }

    @Override
    public long count(Member member, Product product, Boolean isShow) {
        return reviewDao.count(member, product, isShow);
    }

    @Override
    @Transactional
    @CacheEvict(value = {"product", "productCategory", "review", "consultation"}, allEntries = true)
    public void save(Review review) {
        super.save(review);

    }

    @Override
    public Review update(Review review) {
        Review pReview = super.update(review);
        return pReview;
    }

    @Override
    @Transactional
    public Review update(Review review, String... ignoreProperties) {
        return super.update(review, ignoreProperties);
    }

    @Override
    @Transactional
    @CacheEvict(value = {"product", "productCategory", "review", "consultation"}, allEntries = true)
    public void delete(Long id) {
        super.delete(id);
    }

    @Override
    @Transactional
    @CacheEvict(value = {"product", "productCategory", "review", "consultation"}, allEntries = true)
    public void delete(Long... ids) {
        super.delete(ids);
    }

    @Override
    @Transactional
    @CacheEvict(value = {"product", "productCategory", "review", "consultation"}, allEntries = true)
    public void delete(Review review) {
        if (review != null) {
            super.delete(review);

        }
    }

    @Override
    public void create(Member member, OrderItem oi, Integer score, String content, String remoteAddr) {
        Review review = new Review();
        review.setIp(remoteAddr);
        review.setIsShow(true);
        review.setScore(score);
        review.setMember(member);
        review.setContent(content);
        oi.setHasReview(true);
        orderItemDao.merge(oi);
        save(review);
    }


}